package com.wujilin.okra.activity;

import static com.wujilin.okra.Okra.EXTRA_CATEGORY_ID;
import static com.wujilin.okra.Okra.EXTRA_LOADER_ID;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_QUARTER;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_YEAR;
import static com.wujilin.okra.Okra.EXTRA_OKR_LIST_TITLE;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_CATEGORY;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_QUARTER;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST_BY_YEAR;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_CHOOSE;
import static com.wujilin.okra.Okra.REQUEST_OKRA;
import static com.wujilin.okra.Okra.REQUEST_OKR_DELETE;
import static com.wujilin.okra.Okra.TAG_DIALOG_CATEGORY_EDIT;
import static com.wujilin.okra.Okra.TAG_DIALOG_OBJECTIVE_NEW;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.wujilin.okra.R;
import com.wujilin.okra.fragment.CategoryEditFragment;
import com.wujilin.okra.fragment.ObjectiveNewFragment;
import com.wujilin.okra.fragment.OkraListFragment;

/**
 * @author Belin Wu
 */
public class OkraListActivity extends SingleFragmentActivity implements OkraListFragment.Callback, 
		CategoryEditFragment.Callback, ObjectiveNewFragment.Callback {
	private int loaderId;
	private long categoryId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar actionBar = getActionBar();
		
		Intent intent = getIntent();
		String title = intent.getStringExtra(EXTRA_OKR_LIST_TITLE);
		actionBar.setTitle(title);
	}
	
    @Override
    public Fragment onCreateFragment() {
    	OkraListFragment fragment = new OkraListFragment();
    	
    	Intent intent = getIntent();
    	loaderId = intent.getIntExtra(EXTRA_LOADER_ID, LOADER_OKRA_LIST);
    	
    	Bundle args = new Bundle();
    	args.putInt(EXTRA_LOADER_ID, loaderId);
    	switch (loaderId) {
    	case LOADER_OKRA_LIST:
    		break;
    	case LOADER_OKRA_LIST_BY_CATEGORY:
    		categoryId = intent.getLongExtra(EXTRA_CATEGORY_ID, 0);
    		args.putLong(EXTRA_CATEGORY_ID, categoryId);
    		break;
    	case LOADER_OKRA_LIST_BY_QUARTER:
    		int quarter = intent.getIntExtra(EXTRA_OBJECTIVE_QUARTER, 0);
    		args.putInt(EXTRA_OBJECTIVE_QUARTER, quarter);
    	case LOADER_OKRA_LIST_BY_YEAR:
    		int year = intent.getIntExtra(EXTRA_OBJECTIVE_YEAR, 0);
    		args.putInt(EXTRA_OBJECTIVE_YEAR, year);
    		break;
    	}
    	fragment.setArguments(args);
    	
        return fragment;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	if (loaderId != LOADER_OKRA_LIST_BY_CATEGORY) {
    		return false;
    	}
    	getMenuInflater().inflate(R.menu.activity_okra_list, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
	    	case android.R.id.home:
	    		finish();
	    		break;
	    	case R.id.add_okra: {
	    		ObjectiveNewFragment fragment = new ObjectiveNewFragment();
	    		Bundle args = new Bundle();
	    		args.putLong(EXTRA_CATEGORY_ID, categoryId);
	    		
	    		fragment.setArguments(args);
	    		fragment.show(getFragmentManager(), TAG_DIALOG_OBJECTIVE_NEW);
	    		break;
	    	}
	    	case R.id.edit_category:
	    		String category = getActionBar().getTitle().toString();
	    		CategoryEditFragment fragment = CategoryEditFragment.newInstance(categoryId, category);
	    		fragment.show(getFragmentManager(), TAG_DIALOG_CATEGORY_EDIT);
	    		break;
	    	default:
	    		return super.onOptionsItemSelected(item);
    	}
    	return true;
    }
    
	@Override
	@SuppressWarnings("unchecked")
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	switch (requestCode) {
			case REQUEST_OKRA:
			case REQUEST_CATEGORY_CHOOSE:
			case REQUEST_OKR_DELETE:
				Fragment fragment = getFragment(R.id.container);
				fragment.getLoaderManager()
					.restartLoader(loaderId, fragment.getArguments(), (LoaderCallbacks<Cursor>) fragment);
				break;
		}
    }

	@Override
	public void onCategoryRenamed(String category) {
		getActionBar().setTitle(category);
	}

	@Override
	public void onOkraDeleted() {
		onActivityResult(REQUEST_OKRA, Activity.RESULT_OK, null);
	}

	@Override
	public void onOkraMoved() {
		onActivityResult(REQUEST_CATEGORY_CHOOSE, Activity.RESULT_OK, null);
	}
}
